package gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;

import gamePlay.*;


public class MenuCycling {
//public class MenuCycling {
    static JFrame frame = new JFrame();    // frame for GUI displays
    static StartingMenu startScreen;
    static LobbySelectionMenu lobbySelectScreen;
    static LobbyMenu lobbyScreen;
    static JoinLobbyMenu joinLobbyScreen;
    static Game g;
    static Player p1;
    static Player p2;
    static Player p3;
    static Player p4;
    static Player p5;
    static Player p6;
    static ArrayList<Integer> displayAnswers;
    static ArrayList<Integer> sortedAnswers = new ArrayList<Integer>();
    static ArrayList <Integer> odds = new ArrayList<Integer>();
    static ArrayList <Integer> payouts = new ArrayList<Integer>();
    static int num_Players;
    static int round = 0;
   
    
    //static LobbySelectionMenu lobbySelectionScreen;
    //static LobbyMenu hostLobbyScreen;
    static int screenSize = 2;
    public static void main (String[] args)
    {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dim = toolkit.getScreenSize();
       // for testing
       // System.out.println("width = " + dim.width + "   height = " + dim.height);
        if (dim.width < 1440 || dim.height < 900 )
        	screenSize = 1;
        startScreen = new StartingMenu(screenSize);
        lobbySelectScreen = new LobbySelectionMenu(screenSize);
        lobbyScreen = new LobbyMenu(screenSize);
        joinLobbyScreen = new JoinLobbyMenu(screenSize);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(startScreen);
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
        
       // lobbySelectionScreen = new LobbySelectionMenu();
        //hostLobbyScreen = new LobbyMenu();
		
		
    }
    
    // initializes the game state
    public static void startGame(){

    	//integration testing
    	g = new Game("Good Team", num_Players);
		p1 = new Player("User");
	        p1.joinGame(g);
		if (num_Players >= 3){
			p2 = new ComputerPlayer("CPU 1");
			p2.joinGame(g);
			p3 = new ComputerPlayer("CPU 2");
			p3.joinGame(g);
			if (num_Players >= 4){
				p4 = new ComputerPlayer("CPU 3");
				p4.joinGame(g);
					if(num_Players >= 5){
						p5 = new ComputerPlayer("CPU 4");
						p5.joinGame(g);
						if (num_Players == 6){
							p6 = new ComputerPlayer("CPU 5");
							p6.joinGame(g);
						}
					}
			}
		}
//        betScreen = new BetMenu();
//        betTimer = new javax.swing.Timer(1000, betScreen.new TimerListener());
//        resultScreen = new ResultsMenu();
//        resultTimer = new javax.swing.Timer(1000, resultScreen.new TimerListener());
//        winScreen = new WinMenu(screenSize);
//        winTimer = new javax.swing.Timer(1000, winScreen.new TimerListener());
		g.createMenus(screenSize);
        displayQuestionScreen();
	}	
    
    // returns the current question
    public static String getQuestion(){
    	return g.getQuestionGUI();
    }
    
    // returns the correct answer
    public static int getAnswer(){
    	return g.getAnswer();
    }
    
    //returns the Chip count of the Player
    public static int getPlayerChips(){
    	return p1.getGame_chips() + p1.getPermanent_chips();    	
    }
    
    // submit the guesses of all players. Currently uses a static guess for each player except the user
    public static void guess(Integer playerGuess){
        g.clearGuesses();
    	p1.submitGuess(playerGuess);
    	if (num_Players >= 3){
    		p2.submitGuess(g.getQuestion().getAnswer());
        	p3.submitGuess(g.getQuestion().getAnswer());
			if (num_Players >= 4){
				p4.submitGuess(g.getQuestion().getAnswer());
					if(num_Players >= 5){
						p5.submitGuess(g.getQuestion().getAnswer());
						if (num_Players == 6){
							p6.submitGuess(g.getQuestion().getAnswer());
						}
					}
			}
		}		
		displayAnswers = g.answers();
    }
    
     // return the best answer of all answers submitted.
    public static int getBest(){
    	
    	int correct = getAnswer();
       
    	for(int i = sortedAnswers.size() - 1;i >=0;i--)
    		if(sortedAnswers.get(i) <= correct)
    			return sortedAnswers.get(i);
    	return 0;
    	
    }
    
     public static void updateQuestions(){
    	g.updateQuestions();
    }

    //method for calculating the odds on each bet is tiered
    public static void oddsCalc(){
        sortedAnswers.clear();
 
        int total=0;
//    	ArrayList<Integer> sortedAnswers = new ArrayList<Integer>();
    	for(int x= 0; x< displayAnswers.size(); x++)
    	{
    		total += displayAnswers.get(x);
    		sortedAnswers.add(displayAnswers.get(x));
    	}
    	Collections.sort(sortedAnswers);
    	
//    	for(int i = 0; i < sortedAnswers.size(); i++)
//    		System.out.println(sortedAnswers.get(i));
    	
    	//ArrayList <Integer> odds = new ArrayList<Integer>();
    	odds.add(total/sortedAnswers.get(0));
    	odds.add(total/sortedAnswers.get(1));
    	
    	if (displayAnswers.size()>4)
    	{
    		odds.add(total/sortedAnswers.get(2));
    	}
//    	return 0;
    	
    	
    }
    

    
    public static void displayStartScreen(){

/*
 * The following 2 lines were creating a null pointer exception when the user was returning from the select game lobby
 * back to the start screen.    	
 */
//    	g.winTimer.stop();
//    	g.winScreen.seconds = 60;        
        frame.getContentPane().removeAll();
        round = 0;
        // update the frame
        frame.add(startScreen);
        frame.validate();
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
    }
    
    
     // This method will change the frame to display the Question panel
    public static void displayQuestionScreen() {
        
    	g.resultTimer.stop();
    	g.resultScreen.seconds = 60;
        frame.getContentPane().removeAll();
        round++;
        // update the frame
        g.updateQuestions();
        g.questScreen.setQuestion();
        g.questScreen.clearFields();
        g.questTimer.start();
        frame.add(g.questScreen);
        frame.validate();
        frame.pack();
        frame.repaint();
        frame.setVisible(true);

    }   
    
    
public static void displayBetScreen(){
        g.questTimer.stop();
        g.questScreen.seconds = 30;
        g.betScreen.refreshAnswers();
        g.betScreen.setChips();
                
        frame.remove(g.questScreen);
        g.betTimer.start();
        // update the frame
        frame.add(g.betScreen);
        frame.validate();
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
    }
    
    public static void displayResultScreen(){
       
    	MenuCycling.p1.setPermanent_chips(2);
    	
        frame.remove(g.betScreen);
        g.betTimer.stop();
        g.betScreen.seconds = 30;
        
        // update the frame
        if (round == 7){
        	g.winScreen.setText();
        	g.winTimer.start();
        	g.winScreen.refreshAnswers();
        	frame.add(g.winScreen);
        }
        else{
        	g.resultScreen.setText();
        	g.resultTimer.start();
        	g.resultScreen.refreshAnswers();
        	frame.add(g.resultScreen);
        }
        frame.validate();
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
    }
    
    public static void displaySelectLobbyScreen(){
    	 frame.getContentPane().removeAll();
        // update the frame
        frame.add(lobbySelectScreen);
        frame.validate();
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
    }
    
    public static void displayLobbyScreen(){
        frame.remove(lobbySelectScreen);
        // update the frame
        frame.add(lobbyScreen);
        frame.validate();
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
    }
    
    public static void displayJoinLobbyScreen(){
        frame.remove(lobbySelectScreen);
        // update the frame
        frame.add(joinLobbyScreen);
        frame.validate();
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
    }
}